package com.echo.controller;

import com.echo.common.utils.AuthorizationUtil;
import com.echo.entity.bean.CommonResult;
import com.echo.entity.vo.UserDetailedInfoVO;
import com.echo.entity.vo.UserMainPageVO;
import com.echo.service.impl.UserServiceImpl;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/***
 * 用户个人中心控制类
 * @date   2021/7/16 0016 16:28
 * @author JackeyTower
 */
@RestController
@RequestMapping("/user/main-page")
public class PersonalCenterController {
    private UserServiceImpl userService;
    private AuthorizationUtil authorizationUtil;

    public PersonalCenterController(UserServiceImpl userService, AuthorizationUtil authorizationUtil) {
        this.userService = userService;
        this.authorizationUtil = authorizationUtil;
    }


    //获取用户主页面信息
    @GetMapping("/info")
    public CommonResult<UserMainPageVO> getUserMainPageInfo(HttpServletRequest request)  {
        //获取到用户Id
        int userId = Integer.parseInt((String) request.getAttribute("uid"));
        //传入用户id，判断权限
        if(!authorizationUtil.authorityJudgement(userId)){
            throw new AccessDeniedException("没有权限");
        };
        //根据id获取信息
        UserMainPageVO userMainPageInfo = userService.getUserMainPageInfo(userId);
        //返回结果
        return new CommonResult<>(20000,"OK",userMainPageInfo);
    }

    //获取用户详细信息
    @GetMapping("/detailed-info")
    public CommonResult<UserDetailedInfoVO> getUserDetailedInfo(HttpServletRequest request){
        //获取到用户ID
        int userid = (int) request.getAttribute("uid");
        //根据id获取详细信息
        UserDetailedInfoVO userDetailedInfo = userService.getUserDetailedInfo(userid);
        //返回结果
        return new CommonResult<>(20000,"OK",userDetailedInfo);
    }

    //获取用户所创话题（远程调用话题模块的接口）


}
